<?php

/**
 * Description of blockTimeCreateAction
\* www.teuker.nl - mail@teuker.nl
 * 
 * @author francisc
 */

class blockTimeCreateAction extends sfAction {

    public function execute($request) {
        $this->form = new BlockTimeForm();

        if ($this->processBlockTime($request, $this->form)) {
            SqlEngineCore::get()->insert(
                    array('table' => 'settings_dead_time',
                          'columns' => array(
                                'restaurant_id' => array('value' => $this->getUser()->getAttribute('current_restaurant_id'), 'type' => PDO::PARAM_INT),
                                'date_start' => $this->form->getValue('date_start'),
                                'date_end' => $this->form->getValue('date_end'),
                                'time_start' => $this->form->getValue('time_start_hour') . ':' . $this->form->getValue('time_start_minute'),
                                'time_end' => $this->form->getValue('time_end_hour') . ':' . $this->form->getValue('time_end_minute'),
                                'reason' => $this->form->getValue('reason'))));

            $this->error = 0;
        }
        else {
            $taintedValues = $this->form->getTaintedValues();
            $this->form->setTaintedValue('date_end', Util::convertDateServerClient($taintedValues['date_end'], 'nl'));
            $this->form->setTaintedValue('date_start', Util::convertDateServerClient($taintedValues['date_start'], 'nl'));
            $this->error = 1;
        }
    }

    protected function processBlockTime($request) {
        $params = $request->getParameter($this->form->getName());
        $params['date_start'] = Util::convertDateClientServer($params['date_start'], 'nl');
        $params['date_end'] = Util::convertDateClientServer($params['date_end'], 'nl');

        $this->form->bind($params);

        return $this->form->isValid();
    }
}